﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interop;

namespace XMouse360
{
    /// <summary>
    /// Interaction logic for Switcher.xaml
    /// </summary>
    public partial class Switcher : Window
    {

        [System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig=false)]
        public extern static bool DwmIsCompositionEnabled();
		
		[System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig=false)]
		static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

		struct MARGINS
		{
			public MARGINS(Thickness t)
			{
				Left = (int)t.Left;
				Right = (int)t.Right;
				Top = (int)t.Top;
				Bottom = (int)t.Bottom;
			}
			public int Left;
			public int Right;
			public int Top;
			public int Bottom;
		}

        public Switcher()
        {
            InitializeComponent();
			SourceInitialized += new EventHandler(Switcher_SourceInitialized);
			SizeChanged += new SizeChangedEventHandler(Switcher_SizeChanged);
        }


		void Switcher_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			/*System.Drawing.Rectangle rect = System.Windows.Forms.Screen.GetWorkingArea(new System.Drawing.Point((int)Left, (int)Top));
			Top = rect.Height / 2 - this.Height / 2;
			Left = rect.Width / 2 - this.Width / 2;*/
		}

		void Switcher_SourceInitialized(object sender, EventArgs e)
		{
			ExtendGlassFrame(this, new Thickness(-1));
			


		}


		public bool ExtendGlassFrame(Window window, Thickness margin)
		{
			if (!DwmIsCompositionEnabled())
				return false;

			IntPtr hwnd = new WindowInteropHelper(window).Handle;
			if (hwnd == IntPtr.Zero)
				throw new InvalidOperationException("The Window must be shown before extending glass.");

			// Set the background to transparent from both the WPF and Win32 perspectives
			window.Background = Brushes.Transparent;
			HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

			MARGINS margins = new MARGINS(margin);
			DwmExtendFrameIntoClientArea(hwnd, ref margins);
			return true;
		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

    }
}
